﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities;
using Sirenix.Utilities.Editor;
using UnityEditor;

using System;
using System.Reflection;
using Object = UnityEngine.Object;

public class ItemFieldSettingEditorWindow :EditorWindow
{
    [MenuItem("Tools/场景物体参数设置")]
    public static void Show()
    {
       var window= EditorWindow.GetWindow<ItemFieldSettingEditorWindow>();
       
        window.position = GUIHelper.GetEditorWindowRect().AlignCenter(400, 400);
    }
    Vector2 lastScrollPos = Vector2.zero;
    Dictionary<object, bool> isFold=new Dictionary<object, bool>();
    private void OnEnable()
    {
        lastScrollPos = Vector2.zero;
   ///     isFold = 
    }
   
    private void OnGUI()
    {
        string lastGroup = default;
        var objs = ItemParamEditorTool.GetPrefab();
        lastScrollPos= EditorGUILayout.BeginScrollView(lastScrollPos);
        //   GUILayout.Label("----------");
        foreach (var x in objs)
        {
            if (x.Group != lastGroup)
            {
                if (lastGroup != default)
                {

                    EditorGUILayout.Space();
                    EditorGUILayout.Space();
                }
                EditorGUILayout.LabelField("Group", x.Group);
                lastGroup = x.Group;
            }


            var field = x.Object.GetComponent<Item>().ItemFieldSetting;
            field.ItemName = x.Object.name;
            var fields =new List<FieldInfo>( field.GetType().GetFields());
            fields.Reverse();

            
            //    var selectedTransform = Selection.activeGameObject.transform;
            bool fold = isFold.ContainsKey(x.Object)?isFold[x.Object]:false;
            isFold[x.Object]=fold = EditorGUILayout.InspectorTitlebar(fold, x.Object);
            if (fold == false)
            {
                continue;
            }
            //  EditorGUILayout.InspectorTitlebar();
            //    EditorGUILayout.InspectorTitlebar(false, new UnityEngine.Object[] { x.Object });
            //        EditorGUILayout.LabelField(nameof(field.ItemName), field.ItemName);
            bool flag = false;
           flag|=LayoutObject(x.Object.GetComponent<Item>().ItemFieldSetting);
            flag|=LayoutObject(x.Object.GetComponent<Item>());
            var funcs = x.Object.GetComponents<ItemFunc>();
            foreach(var y in funcs)
            {
               flag|= LayoutObject(y);
            }
            if (flag)
            {
                EditorUtility.SetDirty(x.Object);
                AssetDatabase.SaveAssets();
            }
            //    EditorGUILayout.EndHorizontal();
        }
        EditorGUILayout.EndScrollView();
    //    AssetDatabase.SaveAssets();
  //      AssetDatabase.Refresh();
    //    AssetDatabase.Refresh();
    }
    public bool LayoutObject(object mono)
    {
        if (mono is Object)
        {
            bool fold = isFold.ContainsKey(mono) ? isFold[mono] : false;
            isFold[mono] = fold = EditorGUILayout.InspectorTitlebar(fold,mono as Object);
            if (fold == false)
            {
                return false;
            }
        }
        else
        {
        //    EditorGUILayout.LabelField(nameof(mono));
        }
        var fields = mono.GetType().GetFields(BindingFlags.Public|BindingFlags.Instance);
        bool needSave = false;
        foreach(var x in fields)
        {
            needSave|= LayoutField(x, mono);
        }
        if (needSave)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public bool LayoutField(FieldInfo param, object field)
    {
        if (param.IsDefined<HideInInspector>(true))
        {
            return false;
        }
        var tp = param.GetValue(field);

        var name = param.IsDefined<HeaderAttribute>(true) ? param.GetAttribute<HeaderAttribute>().header : param.Name;
        bool flag = true;
        if (param.FieldType == typeof(string))
        {
            param.SetValue(field, EditorGUILayout.TextField(name, (string)param.GetValue(field)));
        }

        else if (param.FieldType == typeof(float)) {
            //        EditorGUILayout.field
            param.SetValue(field, EditorGUILayout.FloatField(name, (float)param.GetValue(field)));
        }else if(param.FieldType == typeof(bool))
        {
            param.SetValue(field, EditorGUILayout.Toggle(name, (bool)param.GetValue(field)));
        }else if (param.FieldType.IsEnum)
        {
            param.SetValue(field, EditorGUILayout.EnumPopup(name,(Enum) param.GetValue(field)));
        }else if (param.FieldType == typeof(int))
        {
            param.SetValue(field, EditorGUILayout.IntField(name, (int)param.GetValue(field)));
        }
        else
        {
            flag = false;
        }
        if (flag&&tp.Equals(param.GetValue(field))==false)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
